function spotlight_select_image(oObj, iMediaId) {

	if (oSpotlight.imageSel !== null) {
		$.post('/admin/media_ajax/get_media_props', { iMediaId : iMediaId }, function(data) {
		
			var sImgSrc = '/assets/media/thumbs/' + data.media_subfolder + '/' + data.media_id + '.' + data.extension_ext;
		
			$(oSpotlight.imageSel).attr('src', sImgSrc).prev().val(data.media_id);
		
			oSpotlight.imageSel = null;
			
			$('#overlay_id').remove();
			
		}, 'json');
	}
	
}


function check_spotlight_images_length() {
	if ($('.spotlight_image_main_cont').length === 0) {
		$('#spotlight_no_spotlights').show();
		$('#spotlight_images_cont').hide();
	} else {
		$('#spotlight_no_spotlights').hide();
		$('#spotlight_images_cont').show();
	}
	
}



function set_spotlight_text_sortable(oObj) {
	<?php
	
	if ($this->config->item('spotlight_images_text_count') > 1)
	{
		?>
		var oOptions = {
			tolerance : 'pointer',
			opacity : 0.5
		};
			
		if (!oObj) {
			oObj = '.spotlight_image_text';
		}
		
		$(oObj).sortable(oOptions);
		<?php
	}
	
	?>
}




$(document).ready(
	function() {
	
		check_spotlight_images_length();
		
		$('#spotlight_images_cont').sortable({
			handle : '.spotlight_image_handle',
			tolerance : 'pointer',
			scroll : true,
			opacity : 0.5
		});
		
		set_spotlight_text_sortable();
		
		if ($('#spotlight_images_cont').html() !== '') {
			$('#spotlight_images_cont').show();
		}
		
		
		$('.text_expanded').live('click', function(e) {
			if ($(this).attr('checked') === true) {
				$(this).parent().parent().parent().parent().find('.text_expanded:checked').removeAttr('checked');
				$(this).attr('checked', 'checked');
			}
		});
		
		
		$('.spotlight_image_remove').live('click', function(e) {
			$(this).parent().slideUp(500, function() {
				$(this).remove();
				check_spotlight_images_length();
				
				
			});
		});
		
		
		$('.spotlight_image_image_tab').live('click', function(e) {
			var oObj = $(this).parent().next().children('.spotlight_image_image');
			
			if ($(oObj).css('display') === 'none') {
				$(this).next().removeClass('spotlight_image_tab_selected');
				$(oObj).show();
				$(this).addClass('spotlight_image_tab_selected');
				$(oObj).next().hide();
			}
		});
		
		$('.spotlight_image_text_tab').live('click', function(e) {
			var oObj = $(this).parent().next().children('.spotlight_image_text');
			
			if ($(oObj).css('display') === 'none') {
				$(this).prev().removeClass('spotlight_image_tab_selected');
				$(oObj).show();
				$(this).addClass('spotlight_image_tab_selected');
				$(oObj).prev().hide();
			}
		});
		
		
		$('#spotlight_add_new').bind('click', function(e) {
			
			$.post('/admin/spotlights_ajax/get_spotlight_image_template', {}, function(data) {
				$('#spotlight_images_cont').append(data.sContent);
				
				if ($('.spotlight_image_main_cont').length === 0) {
					$('#spotlight_images_cont').hide();
				} else {
					$('#spotlight_images_cont').show();
				}
				
				set_spotlight_text_sortable($('.spotlight_image_text:last'));
				
			}, 'json');
			
		});
		
		
		$('.spotlight_select_img').live('click', function(e) {
			oSpotlight.imageSel = $(this).prev();
			
			create_popup('iframe_id', 'overlay_id', '962', '500', 'Test title', '/admin/media/popup/spotlight_select_image', function(oObj) {
				$('#overlay_id').show();
			});
		});
		
		
		
		
		$('.text_link').live('click', function(e) {
			var oObj = this;
			
			create_popup('spotlight_int_url_iframe', 'iframe_overlay', 350, 500, 'Select a page to link to', '/admin/pages/get_pages_tree', function() {
				var oContents = $('#spotlight_int_url_iframe').contents();
					
				$(oContents).find('.page_struct').treeview({
					persist: 'location',
					collapsed: true,
					unique: true
				});
					
				$(oContents).find('#confirm_button').bind('click', function() {
					var oInput = $(oContents).find(':radio[name="page_clone_id"]:checked');
						
					if (oInput.length === 0) {
						$.prompt('Please select a page to link to');
						return;
					}
					
					$.post('/admin/pages_ajax/get_page_details', { iPageId : $(oInput).val() }, function(data) {
						$(oObj).parent().next().html(data.page_menu_title + ' (/' + data.lang_accr + '/' + data.page_name + ')').next().val(data.page_id);
					}, 'json');
					
					
					$('#iframe_overlay').remove();

				});
					
				$('#iframe_overlay').show();
						
			});		
		});
		
		
		
		$('#spotlight_save_button').bind('click', function(e) {
	
			
			// get the spotlight title
			var sSpotlightTitle = $('#spotlight_title').val();
			var sVersionActive = (($('#version_active:checked').length === 1) ? 'Y' : 'N');
			
			if (sSpotlightTitle === '') {
				$.prompt('<?php echo addslashes($aMessages['admin_spotlights_enter_title']); ?>');
				return;
			}
			
			var iImgCount = 0;
			
			// check to see if each spotlight has an image
			$('.spotlight_image_image').children('img').each(
				function() {
					if ($(this).attr('src') === '') {
						iImgCount++;
					}
				}
			);
			
			if (iImgCount > 0) {
				$.prompt('<?php echo addslashes($aMessages['admin_spotlights_select_spotlight_image']); ?>');
				return;
			}

			// firstly, get the new version number
			$.post('/admin/spotlights_ajax/get_version_number', { iVersionId : oSpotlight.version_id, sSpotlightTitle : sSpotlightTitle, sVersionActive : sVersionActive }, function(data) {
				
				var iVersionId = data.iVersionId;
				
				// now recursively, add each image, and then each relevant text blocks
				
							
				$('.spotlight_image_cont').each(
					function(iImagePos) {
						// set our global oObj
						var oObj = this;
			
						var oParams = { 
							iVersionId : iVersionId,
							iMediaId : $(oObj).contents().find('input[name="media_id[]"]').val(),
							iImagePos : iImagePos
						};
						
						$.post('/admin/spotlights_ajax/add_images_to_version', oParams, function(data) {
							var iImageId = data.iImageId;
							
							// now recursively, add each text block
							
							var iTextPos = 0;
							
							$(oObj).contents().find('.spotlight_image_text_cont').each(
								function(iTextPos) {
									 var oTextParams = {
									 	iImageId : iImageId,
										sTextHeader : $(this).find('.text_header').val(),
										sTextContent : $(this).find('.text_content').val(),
										iPageId : $(this).find('.page_id').val(),
										iTextPos : iTextPos
										/*
										// TODO: get the user to select a page to link to
										,
										iPageId : 0
										*/
									 }
									 
									 $.post('/admin/spotlights_ajax/add_text_to_image', oTextParams, function(data) {
									 
									 }, 'json');
								}
							);
							
							
							
						}, 'json');
						

					}
				);
						
			
			}, 'json');
		

		});

	}
);